package com.css.zfzx.sjcj.modules.yhdisaster.repository.impl;

import com.css.zfzx.sjcj.modules.yhdisaster.repository.YhAnalysisQueryRepository;
import com.css.zfzx.sjcj.modules.yhdisaster.repository.entity.YhDisasterEntity;
import com.css.zfzx.sjcj.modules.yhdisaster.viewobjects.AnalysisQueryVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Repository
public class YhAnalysisQueryRepositoryImpl implements YhAnalysisQueryRepository {
    @PersistenceContext
    private EntityManager em;
    @Override
    public Integer queryCountAnalysis(AnalysisQueryVo analysisQueryVo) {
        StringBuilder sql = new StringBuilder("select count(*) from yh_disaster ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!StringUtils.isEmpty(analysisQueryVo.getProvince())){
            whereSql.append(" and province = :province");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getArea())){
            whereSql.append(" and area =:area");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getCity())){
            whereSql.append(" and city = :city");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhEarthFortification())){
             whereSql.append(" and yh_earth_fortification =:yhEarthFortification ");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhEarthIntensity())){
            whereSql.append(" and yh_earth_intensity =:yhEarthIntensity");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhNowEarthQh())){
            whereSql.append(" and yh_now_earth_qh =:yhNowEarthQh ");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhFieldType())){
            whereSql.append(" and yh_field_type =:yhFieldType");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhLotType())){
            whereSql.append(" and yh_lot_type =:yhLotType");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhExistingVirus())){
            whereSql.append(" and yh_existing_virus =:yhExistingVirus");
        }
        whereSql.append(" and yh_project_type = :type");
        whereSql.append(" and is_valid = 1");
        Query countQuery = this.em.createNativeQuery(sql.append(whereSql).toString());
        if(!StringUtils.isEmpty(analysisQueryVo.getProvince())){
            countQuery.setParameter(" province",analysisQueryVo.getProvince());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getArea())){
            countQuery.setParameter(" area",analysisQueryVo.getArea());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getCity())){
            countQuery.setParameter(" city",analysisQueryVo.getCity());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhEarthFortification())){
            countQuery.setParameter("yhEarthFortification",analysisQueryVo.getYhEarthFortification());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhEarthIntensity())){
            countQuery.setParameter("yhEarthIntensity",analysisQueryVo.getYhEarthIntensity());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhNowEarthQh())){
            countQuery.setParameter("yhNowEarthQh",analysisQueryVo.getYhNowEarthQh());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhFieldType())){
            countQuery.setParameter("yhFieldType",analysisQueryVo.getYhFieldType());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhLotType())){
            countQuery.setParameter("yhLotType",analysisQueryVo.getYhLotType());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getYhExistingVirus())){
            countQuery.setParameter("yhExistingVirus",analysisQueryVo.getYhExistingVirus());
        }
        countQuery.setParameter("type",analysisQueryVo.getType());
        Integer singleResult =(Integer) countQuery.getSingleResult();
        return singleResult;
    }

    @Override
    public List<YhDisasterEntity> queryFortificationBig(AnalysisQueryVo analysisQueryVo) {
        StringBuilder sql = new StringBuilder("select count * from yh_disaster ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!StringUtils.isEmpty(analysisQueryVo.getProvince())){
            whereSql.append(" and province = :province");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getArea())){
            whereSql.append(" and area =:area");
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getCity())){
            whereSql.append(" and city = :city");
        }
        whereSql.append(" and yh_project_type = :type");
        whereSql.append(" and is_valid = 1");
        Query countQuery = this.em.createNativeQuery(sql.append(whereSql).toString(), YhDisasterEntity.class);
        if(!StringUtils.isEmpty(analysisQueryVo.getProvince())){
            countQuery.setParameter(" province",analysisQueryVo.getProvince());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getArea())){
            countQuery.setParameter(" area",analysisQueryVo.getArea());
        }
        if(!StringUtils.isEmpty(analysisQueryVo.getCity())){
            countQuery.setParameter(" city",analysisQueryVo.getCity());
        }
        countQuery.setParameter("type",analysisQueryVo.getType());
        List<YhDisasterEntity> list=countQuery.getResultList();
        return list;
    }
}
